package com.work.keep.ui;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.work.keep.R;

/**
 * Created by dllo on 16/1/26.
 */
public class PullToRefreshCoordinatorLayout extends PullToRefreshBase<CoordinatorLayout> {
    private CoordinatorLayout coordinatorLayout;
    private View v;
    private LoadingLayout mLoadMoreFooterLayout;

    public PullToRefreshCoordinatorLayout(Context context) {
        this(context, null);
    }

    @Override
    protected void startLoading() {
        super.startLoading();

        if (null != mLoadMoreFooterLayout) {
            mLoadMoreFooterLayout.setState(ILoadingLayout.State.REFRESHING);
        }
    }

    public View getV() {
        return v;
    }

    @Override

    public void onPullUpRefreshComplete() {
        super.onPullUpRefreshComplete();

        if (null != mLoadMoreFooterLayout) {
            mLoadMoreFooterLayout.setState(ILoadingLayout.State.RESET);
        }
    }

    private OnReadForLoading onReadForLoading;

    public void setOnReadForLoading(OnReadForLoading onReadForLoading) {
        this.onReadForLoading = onReadForLoading;
    }

    public PullToRefreshCoordinatorLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullToRefreshCoordinatorLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setPullLoadEnabled(false);
    }

    @Override
    public LoadingLayout getFooterLoadingLayout() {
        if (isScrollLoadEnabled()) {
            //返回已经处理好的LoadMoreFooterLayout
            return mLoadMoreFooterLayout;
        }

        return super.getFooterLoadingLayout();
    }

    @Override
    protected CoordinatorLayout createRefreshableView(Context context, AttributeSet attrs) {
        v = LayoutInflater.from(context).inflate(R.layout.activity_people, null);
        coordinatorLayout = (CoordinatorLayout) v.findViewById(R.id.peoplePullToRefresh);
        return coordinatorLayout;
    }

    @Override
    protected LoadingLayout createHeaderLoadingLayout(Context context, AttributeSet attrs) {
        return new RotateLoadingLayout(context);
    }

    @Override
    protected boolean isReadyForPullUp() {
        return isLastItemVisible();
    }


    @Override
    protected boolean isReadyForPullDown() {
        return isFirstItemVisible();
    }

    private boolean isFirstItemVisible() {
        int mostTop = (coordinatorLayout.getChildCount() > 0) ? coordinatorLayout.getChildAt(0).getTop() : 0;
        if (mostTop >= 0) {
            return true;
        }

        return false;
    }

    private boolean isLastItemVisible() {
        if (onReadForLoading != null) {
            if (onReadForLoading.isRead()) {
                return true;
            }
        }
        return false;
    }

    public void setHasMoreData(boolean hasMoreData) {

        if (!hasMoreData) {
            if (null != mLoadMoreFooterLayout) {
                mLoadMoreFooterLayout.setState(ILoadingLayout.State.NO_MORE_DATA);
            }
            //此部分是否必须???
            LoadingLayout footerLoadingLayout = getFooterLoadingLayout();
            if (null != footerLoadingLayout) {
                footerLoadingLayout.setState(ILoadingLayout.State.NO_MORE_DATA);
            }
        }
    }
}
